package cn.com.thingo.shyintelligent.ui.activity;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;

import butterknife.BindView;
import butterknife.OnClick;
import cn.com.thingo.shyintelligent.R;
import cn.com.thingo.shyintelligent.base.BaseActivity;
import cn.com.thingo.shyintelligent.base.contract.LoginContract;
import cn.com.thingo.shyintelligent.model.bean.LoginBean;
import cn.com.thingo.shyintelligent.model.db.UserInfo;
import cn.com.thingo.shyintelligent.presenter.LoginPresenter;
import cn.com.thingo.shyintelligent.util.SPUtils;
import cn.com.thingo.shyintelligent.util.ToastUtil;
import cn.com.thingo.shyintelligent.widget.ClearEditText;

public class LoginActivity extends BaseActivity<LoginPresenter> implements LoginContract.View, View.OnClickListener {
    @BindView(R.id.login_phone_et)
    ClearEditText etPhone;
    @BindView(R.id.login_password_et)
    EditText etPassword;
    @BindView(R.id.login_remember_account_password)
    LinearLayout llRemember;
    @BindView(R.id.login_remember_radio)
    ImageView ivRemember;
    private String phone;
    private String password;

    @OnClick({R.id.login_remember_account_password, R.id.login_remember_radio, R.id.login_register, R.id.login_submit, R.id.login_forget_password})
    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.login_remember_account_password:
                ivRemember.setImageResource((boolean) (ivRemember.getTag()) ? R.mipmap.login_radio_unchecked : R.mipmap.login_radio_checked);
                ivRemember.setTag(!(boolean) (ivRemember.getTag()));
                break;
            case R.id.login_register:
                Intent registerIntent = new Intent(LoginActivity.this, RegisterActivity.class);
                startActivity(registerIntent);
                break;
            case R.id.login_submit:
                phone = etPhone.getText().toString().trim();
                password = etPassword.getText().toString().trim();
                if (TextUtils.isEmpty(phone)) {
                    ToastUtil.shortShow("手机号码不能为空！");
                } else if (TextUtils.isEmpty(password)) {
                    ToastUtil.shortShow("密码不能为空！");
                } else {
                    mPresenter.login(password, phone);
                }
                break;
            case R.id.login_forget_password:
                Intent forgetIntent = new Intent(mContext, ForgetPasswordActivity.class);
                startActivity(forgetIntent);
                break;
        }
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    protected int getLayout() {
        return R.layout.activity_login;
    }

    @Override
    protected void initEventAndData() {
        ivRemember.setTag(false);
        String phone = SPUtils.newInstance().getString(SPUtils.ACCOUNT_PHONE);
        String password = SPUtils.newInstance().getString(SPUtils.ACCOUNT_PASSWORD);
        if (phone != null && password != null) {
            ivRemember.setTag(true);
            ivRemember.setImageResource(R.mipmap.login_radio_checked);
            etPhone.setText(phone);
            etPassword.setText(password);
        }
    }

    @Override
    public void viewLogin(LoginBean loginBean) {
        boolean isRememberAccount = (boolean) ivRemember.getTag();
        if (isRememberAccount) {
            SPUtils.newInstance().put(SPUtils.ACCOUNT_PHONE, phone);
            SPUtils.newInstance().put(SPUtils.ACCOUNT_PASSWORD, password);
        } else {
            SPUtils.newInstance().remove(SPUtils.ACCOUNT_PHONE);
            SPUtils.newInstance().remove(SPUtils.ACCOUNT_PASSWORD);
        }
        //todo 推送消息暂未绑定
        LoginBean.DataBean data = loginBean.getData();
        SPUtils.newInstance().put(SPUtils.LOGIN_TOKEN, data.getToken());
        SPUtils.newInstance().put(SPUtils.LOGIN_USERNAME, data.getPhone());
        UserInfo userInfo = UserInfo.getInstance();
        userInfo.setToken(data.getToken());
//        userInfo.setAccountId(data.getAccount_id());
//        userInfo.setUserName(data.getTelephone());
        Intent intent = new Intent(mContext, SelectGatewayActivity.class);
        startActivity(intent);
        SPUtils.newInstance().remove(SPUtils.CURRENT_ROOM_ID);
        finish();
    }
}